package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.csd.CustomerMemberCardDetailCustomMapper;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.*;
import com.crm.dao.mapper.customMapper.opportunityManagerCustomMapper.OpportunityCustomMapper;
import com.crm.dao.mapper.customerManager.*;
import com.crm.dao.mapper.opportunityManager.OpportunityMapper;
import com.crm.model.entity.customerManager.*;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.AnalyseCustomerQO;
import com.crm.model.qo.customerManager.CustomerQO;
import com.crm.model.qo.customerManager.MoveCustomerQO;
import com.crm.model.qo.opportunityManager.OpportunityQO;
import com.crm.model.vo.contactManager.IndustryAreaVO;
import com.crm.model.vo.csd.customerManager.CustomerMemberCardDetailVO;
import com.crm.model.vo.customerManager.*;
import com.crm.model.vo.datasearch.DataSearchResultVO;
import com.crm.model.vo.opportunityManager.OpportunityCustomerVO;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.customerManager.CustomerProductDetailMemberCardDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * @ Author ：bj
 * @ Date   ：2021/7/1 10:03
 */
@Service
public class CustomerProductDetailMemberCardDbServiceImpl implements CustomerProductDetailMemberCardDbService {

    @Autowired
    private CustomerProductDetailMemberCardMapper customerProductDetailMemberCardMapper;

    @Autowired
    private CustomerProductDetailMemberCardRawDataMapper customerProductDetailMemberCardRawDataMapper;

    @Autowired
    private CustomerProductDetailMemberCardRawDataCustomMapper customerProductDetailMemberCardRawDataCustomMapper;

    @Autowired
    private CustomerMemberCardDetailCustomMapper customerMemberCardDetailCustomMapper;


    /**
     * 查询会员卡信息
     * @param memberCardNumber 会员卡号
     * @param accountId 所有人id
     * @param customerId 客户id
     * @param contractId 合同id
     * @param productOrderId 订单id
     * @param companyId 公司id
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard selectCustomerProductDetailMemberCard(String memberCardNumber, String accountId, String customerId, String contractId, String productOrderId, String companyId) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        criteria.andMemberCardNumberEqualTo(memberCardNumber);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContractIdEqualTo(contractId);
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andCompanyIdEqualTo(companyId);
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 通过会员卡编号查询
     * @param memberCardNumber 会员卡号
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard getCustomerProductDetailMemberCard(String memberCardNumber) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        criteria.andMemberCardNumberEqualTo(memberCardNumber);
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 查询导入会员卡的数据
     * @return
     */
    @Override
    public List<CustomerProductDetailMemberCardRawDataVO> selectCardDataRaw() {
        /*CustomerProductDetailMemberCardRawDataExample customerProductDetailMemberCardRawDataExample = new CustomerProductDetailMemberCardRawDataExample();
        CustomerProductDetailMemberCardRawDataExample.Criteria criteria = customerProductDetailMemberCardRawDataExample.createCriteria();
        criteria.andCardNumberNotEqualTo("");*/
        List<CustomerProductDetailMemberCardRawDataVO> list = customerProductDetailMemberCardRawDataCustomMapper.selectCardDataRaw();
        return list;
    }

    /**
     * 查询会员卡没有合同id的
     * @return
     */
    @Override
    public List<CustomerProductDetailMemberCard> selectCardData() {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        //criteria.andDataSourceEqualTo(1);
        //criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        //criteria.andContractIdIsNull();
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        return list;
    }

    /**
     * 修改会员卡信息
     * @param card 会员卡
     */
    @Override
    public Boolean updateCard(CustomerProductDetailMemberCard card) {
        Boolean result = false;
        int num = customerProductDetailMemberCardMapper.updateByPrimaryKeySelective(card);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过id查询会员卡信息
     * @param customerProductDetailMemberCardId 会员卡id
     * @return
     */
    @Override
    public CustomerProductDetailMemberCard selectById(String customerProductDetailMemberCardId) {
        return customerProductDetailMemberCardMapper.selectByPrimaryKey(customerProductDetailMemberCardId);
    }

    /**
     * 客服管理-客户详情-会员卡信息
     * @param customerId 客户id
     * @return
     */
    @Override
    public List<CustomerProductDetailMemberCard> selectCardByCustomerId(String customerId,String cardNumber) {
        CustomerProductDetailMemberCardExample customerProductDetailMemberCardExample = new CustomerProductDetailMemberCardExample();
        customerProductDetailMemberCardExample.setOrderByClause("end_tm desc");
        CustomerProductDetailMemberCardExample.Criteria criteria = customerProductDetailMemberCardExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        if(!StringUtils.isBlank(cardNumber)) {
            criteria.andMemberCardNumberLike("%" + cardNumber.trim() + "%");
        }
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerProductDetailMemberCard> list = customerProductDetailMemberCardMapper.selectByExample(customerProductDetailMemberCardExample);
        return list;
    }

    /**
     * 新增会员卡
     * @param card 会员卡
     * @return
     */
    @Override
    public Boolean addCustomerProductDetailMemberCard(CustomerProductDetailMemberCard card) {
        Boolean result = false;
        int num = customerProductDetailMemberCardMapper.insertSelective(card);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 根据时间段查询会员卡信息补全accountId
     * @param startTm 开始时间
     * @param endTm 结束时间
     * @return
     */
    @Override
    public List<CustomerProductDetailMemberCard> selectTimeEndCardData(String startTm, String endTm) {
        return customerProductDetailMemberCardRawDataCustomMapper.selectTimeEndCardData(startTm,endTm);
    }

    /**
     * 查询会员卡
     * @param customerProductDetailMemberCardId
     * @return
     */
    @Override
    public CustomerMemberCardDetailVO selectCustomerCard(String customerProductDetailMemberCardId) {
        return customerMemberCardDetailCustomMapper.selectCustomerCard(customerProductDetailMemberCardId);
    }

    /**
     * 修改会员卡信息
     * @param vo
     * @return
     */
    @Override
    public Boolean updateCardXml(CustomerMemberCardDetailVO vo) {
        return customerMemberCardDetailCustomMapper.updateCardXml(vo);
    }
}
